package cn.rui.service.goods;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.rui.common.ContentBean;
import cn.rui.common.ResponseBean;
import cn.rui.mapper.goods.GoodsMapper;
import cn.rui.pojo.goods.Goods;
import cn.rui.pojo.vo.PageVo;
import cn.rui.utils.QiniuUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 徽州大都督
 * @date 2021/6/21
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Value("${default.img.prefix}")
    private String imgPrefix;

    public ResponseBean findPage(PageVo pageVo) {

//        List<String> all = QiniuUtils.findAll ();
//        for (String s : all) {
//            Integer i = 1;
//
//            if (s.contains ("jpg")||s.contains ("png")){
//                Goods goods = new Goods ();
//                goods.setTitle ("商品"+i);
//                goods.setImages ("https://file.qaqavr.com/"+s);
//                goods.setPrice (100L+i);
//                goods.setEnable (true);
//                goods.setCategoryId (1L);
//                goodsMapper.insert (goods);
//                i++;
//            }
//        }

        QueryWrapper<Goods> wrapper = new QueryWrapper<> ();
        Page<Goods> page = new Page<> (pageVo.getCurrentPage (), pageVo.getPageSize ());

        if (StrUtil.isNotEmpty (pageVo.getQueryContent ())) {
            wrapper.like ("title", pageVo.getQueryContent ());
        }

        if (!ObjectUtil.isEmpty (pageVo.getCategoryId ())) {
            wrapper.eq ("category_id", pageVo.getCategoryId ());
        }

        wrapper.orderByDesc ("create_time");

        IPage<Goods> userIPage = goodsMapper.selectPage (page, wrapper);

        PageVo pv = new PageVo ();

        pv.setTotal (userIPage.getTotal ());

        List<Goods> goodsList = userIPage.getRecords ();

        pv.setData (goodsList);

        return ResponseBean.success (ContentBean.QuerySuccess, pv);
    }

    public ResponseBean updateEnable(Long id) {
        Goods goods = goodsMapper.selectById (id);
        if (goods.getEnable ()) {
            goods.setEnable (false);
        } else {
            goods.setEnable (true);
        }
        goodsMapper.updateById (goods);
        return ResponseBean.success (ContentBean.UpdateSuccess);
    }

    public ResponseBean save(Goods goods) {
        goods.setImages (imgPrefix + goods.getImages ());
        goodsMapper.insert (goods);
        return ResponseBean.success (ContentBean.SavaSuccess);
    }

    public ResponseBean deleteById(Long id) {
        Goods goods = goodsMapper.selectById (id);
        int delete = goodsMapper.deleteById (id);

        if (delete > 0) {
            String images = goods.getImages ();
            int index = images.lastIndexOf ("/");
            String imgSrc = images.substring (index + 1);
            QiniuUtils.deleteFileFromQiniu (imgSrc);
        }

        return ResponseBean.success (ContentBean.DeleteSuccess);
    }

    public void updateImage(Long id, String imgName) {
        Goods goods = goodsMapper.selectById (id);
        String images = goods.getImages ();
        int index = images.lastIndexOf ("/");
        String img = images.substring (index + 1);
        QiniuUtils.deleteFileFromQiniu (img);
        goods.setImages (imgPrefix+imgName);
        goodsMapper.updateById (goods);
    }

    public ResponseBean updateGoods(Goods goods) {
        Goods byId = goodsMapper.selectById (goods.getId ());
        goods.setImages (byId.getImages ());
        goodsMapper.updateById (goods);
        return ResponseBean.success (ContentBean.UpdateSuccess);
    }
}
